package com.example.hosptialtag.service;

/**
 * @Author liuyuhao&caiang
 * @Date 2022/2/21
 */

import com.example.hosptialtag.pojo.Bind;
import com.example.hosptialtag.pojo.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import java.util.List;
import java.util.Map;

@Service
public class TagService {
    @Autowired
    private final JdbcTemplate jdbcTemplate;

    private String sql;

    public TagService(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public String createTag(String tag_name, Integer tag_group, String tag_start_time, String tag_end_time,
                            int tag_office, int tag_weight, int oid, int role, Model model){
        String auth_flag = authCreate(tag_group, tag_office, tag_weight, oid, role);
        if(auth_flag != "pass"){ // 权限控制
                model.addAttribute("lt_message", auth_flag);
                return "forward:listAllTags";
        }
        if(tag_name.length()<1||tag_start_time.length()<1||tag_end_time.length()<1){
            model.addAttribute("lt_message","新增标签的信息不能有空");
            return "forward:listAllTags";
        }
        if(1 <= jdbcTemplate.queryForObject("select count(*) from tag " +
                "where tag_name = ? and tag_delete = 0", Integer.class, tag_name)){
            model.addAttribute("lt_message","该标签已存在");
            return "forward:listAllTags";
        }
        if(1 <= jdbcTemplate.queryForObject("select count(*) from tag " +
                "where tag_name = ? and tag_delete != 0", Integer.class, tag_name)){
            jdbcTemplate.update("update tag set tag_delete = 0, tag_group = ?, tag_start_time = ?, tag_end_time = ?, tag_office = ?, tag_weight = ? where tag_name = ?;",
                    tag_group, tag_start_time, tag_end_time, tag_office, tag_weight, tag_name);
            model.addAttribute("lt_message","添加成功");
            return "forward:listAllTags";
        }
        jdbcTemplate.update("insert into tag(tag_name, tag_group, tag_start_time, tag_end_time, tag_office, tag_weight) value (?,?,?,?,?,?)",
                tag_name, tag_group, tag_start_time, tag_end_time, tag_office, tag_weight);
        model.addAttribute("lt_message","添加成功");
        return "forward:listAllTags";
    }


    public String deleteTagById(int tag_id, int tag_group, int tag_office, int tag_weight, int oid, int role, Model model) {
        String auth_flag = authDetele(tag_group, tag_group, tag_office, tag_weight, oid, role);
        if(auth_flag != "pass"){ // 权限控制
            model.addAttribute("lt_message", auth_flag);
            return "forward:listAllTags";
        }
        jdbcTemplate.update("update tag set tag_delete = 1 where tag_id=?", tag_id);
        model.addAttribute("lt_message","删除成功");
        return "forward:listAllTags";
    }


    public String updateTagById(int tag_id, String tag_name, Integer tag_group, String tag_start_time, String tag_end_time,
                            int tag_office, int tag_weight, int oid, int role, Model model) {
        String auth_flag = authUpdate(tag_group, tag_group, tag_office, tag_weight, oid, role);
        tag_name = tag_name.replace(" ", "");
        if(auth_flag != "pass"){ // 权限控制
            model.addAttribute("lt_message", auth_flag);
            return "forward:getTagByIdNoAuth";
        }
        if(tag_name.length()<1||tag_start_time.length()<1||tag_end_time.length()<1){
            model.addAttribute("lt_message","修改标签的信息不能有空");
            return "forward:getTagById";
        }
        Tag tag = getTagById(tag_id);
        if(!(tag.getTag_name().equals(tag_name))) { // 修改名称判断是否重复
            if (1 <= jdbcTemplate.queryForObject("select count(*) from tag " +
                    "where tag_name = ? and tag_delete = 0", Integer.class, tag_name)) {
                model.addAttribute("lt_message", "该标签已存在");
                return "forward:getTagById";
            }
        }
        jdbcTemplate.update("update tag set tag_name = ?, tag_group = ?, tag_start_time = ?, tag_end_time = ?, tag_office = ?, tag_weight = ? where tag_id = ?;",
                tag_name, tag_group, tag_start_time, tag_end_time, tag_office, tag_weight, tag_id);
        model.addAttribute("lt_message","修改成功");
        return "forward:listAllTags";
    }

    public String listAllTags(Model model) {
        model.addAttribute("tagList", jdbcTemplate.queryForList("select * from tag where tag_delete!=1"));//保存所有的Tag
        model.addAttribute("officeList", jdbcTemplate.queryForList("select * from tb_offices"));//保存所有的科室
        model.addAttribute("groupList", jdbcTemplate.queryForList("select * from tb_taggroup"));//保存所有的标签分组
        model.addAttribute("weightList", jdbcTemplate.queryForList("select * from tb_tagweight"));//保存所有的标签权重
        return "tagList";
    }

    public String getTagById(int tag_id, int tag_group, int tag_office, int tag_weight, int oid, int role, Model model) {
        String auth_flag = authEdit(tag_group, tag_office, tag_weight, oid, role);
        if(auth_flag != "pass"){ // 权限控制
            model.addAttribute("lt_message", auth_flag);
            return "forward:listAllTags";
        }
        String sql="select * from tag where tag_id = ? and tag_delete!=1";
        RowMapper<Tag> rowMapper=new BeanPropertyRowMapper<Tag>(Tag.class);
        Tag tag = jdbcTemplate.queryForObject(sql, rowMapper,tag_id);
        model.addAttribute("tag",tag);
        model.addAttribute("officeList", jdbcTemplate.queryForList("select * from tb_offices"));//保存所有的科室
        model.addAttribute("groupList", jdbcTemplate.queryForList("select * from tb_taggroup"));//保存所有的标签分组
        model.addAttribute("weightList", jdbcTemplate.queryForList("select * from tb_tagweight"));//保存所有的标签权重
        return "updateTag";
    }

    public String getTagByIdNoAuth(int tag_id, int tag_group, int tag_office, int tag_weight, int oid, int role, Model model) {
        String sql="select * from tag where tag_id = ? and tag_delete!=1";
        RowMapper<Tag> rowMapper=new BeanPropertyRowMapper<Tag>(Tag.class);
        Tag tag = jdbcTemplate.queryForObject(sql, rowMapper,tag_id);
        model.addAttribute("tag",tag);
        model.addAttribute("officeList", jdbcTemplate.queryForList("select * from tb_offices"));//保存所有的科室
        model.addAttribute("groupList", jdbcTemplate.queryForList("select * from tb_taggroup"));//保存所有的标签分组
        model.addAttribute("weightList", jdbcTemplate.queryForList("select * from tb_tagweight"));//保存所有的标签权重
        return "updateTag";
    }
    public Tag getTagById(int tag_id){
        String sql="select * from tag where tag_id = ?";
        RowMapper<Tag> rowMapper=new BeanPropertyRowMapper<Tag>(Tag.class);
        Tag tag = jdbcTemplate.queryForObject(sql, rowMapper,tag_id);
        return tag;
    }
    public Tag getTagByBindId(int bind_id){
        String sql="select bind_tag_id from bind where bind_id = ?";
        RowMapper<Bind> rowMapperBind =new BeanPropertyRowMapper<Bind>(Bind.class);
        Bind bind = jdbcTemplate.queryForObject(sql, rowMapperBind, bind_id);
        sql="select * from tag where tag_id = ?";
        RowMapper<Tag> rowMapperTag = new BeanPropertyRowMapper<Tag>(Tag.class);
        Tag tag = jdbcTemplate.queryForObject(sql, rowMapperTag, bind.getBind_tag_id());
        return tag;
    }
    public String getTagsByPatientIdAndGroupId(int patient_id, int tag_group, Model model) {
        sql = "select * from tag \n" +
                "where tag_group = ? and tag_id = (\n" +
                "select bind_tag_id from bind\n" +
                "where bind_tag_id = tag_id and bind_patient_id = ? \n" +
                ");";
        List<Map<String, Object>> tag = jdbcTemplate.queryForList(sql, patient_id, tag_group);
        System.out.println(tag.toString());
        model.addAttribute("tag",tag);
        return "index";
    }

    public String getTagByName(String tag_name, Model model) {
        sql = "select * from tag where tag_name like ?";
        model.addAttribute("tagList", jdbcTemplate.queryForList(sql,'%'+tag_name+'%'));//保存所有的Tag
        model.addAttribute("officeList", jdbcTemplate.queryForList("select * from tb_offices"));//保存所有的科室
        model.addAttribute("groupList", jdbcTemplate.queryForList("select * from tb_taggroup"));//保存所有的标签分组
        model.addAttribute("weightList", jdbcTemplate.queryForList("select * from tb_tagweight"));//保存所有的标签权重
        return "tagList";
    }

    public String getTagsCount(Model model) {
        model.addAttribute("count",Integer.toString(jdbcTemplate.queryForObject("select count(1) from tag where tag_delete != 1", Integer.class)));
        return "index";
    }
    public int getTagsCountByTagId(int tag_id) {
        return jdbcTemplate.queryForObject("select count(1) from tag where tag_delete != 1 and tag_id = "+ Integer.toString(tag_id), Integer.class);
    }

    private String authCreate(int tag_group, int tag_office, int tag_weight, int oid, int role) {// 创建标签权限控制
        if(tag_group > role+1){// 分组
            return "您无法在该分组下创建标签";
        }
        if(tag_office == 8 || tag_group == 2 || tag_group == 3 || tag_weight == 3){// 基础信息
            if(tag_office != 8 || (tag_group != 2 && tag_group != 3) || tag_weight != 3){
                return "创建基础信息标签或业务标签，需要选择分组：病人基础属性/业务属性；科室：无科室；分类：基础信息";
            }
        }
        if(tag_office != oid && tag_office != 8){// 科室
            return "您只能创建您所在科室的标签";
        }
        if(role+1 < tag_weight){
            return "权限不足";
        }
        return "pass";
    }

    private String authDetele(int tag_group, int tag_group1, int tag_office, int tag_weight, int oid, int role) {// 删除标签权限控制
        if(tag_group > role+1){// 分组
            return "您无法删除该分组下标签";
        }
        if(tag_office != oid && tag_office != 8){// 科室
            return "您只能删除您所在科室的标签";
        }
        if(role+1 < tag_weight){
            return "权限不足";
        }
        return "pass";
    }

    private String authUpdate(Integer tag_group, Integer tag_group1, int tag_office, int tag_weight, int oid, int role) {
        if(tag_group > role+1){// 分组
            return "您无法在将标签该分组下";
        }
        if(tag_office == 8 || tag_group == 2 || tag_group == 3 || tag_weight == 3){// 基础信息
            if(tag_office != 8 || (tag_group != 2 && tag_group != 3) || tag_weight != 3){
                return "基础信息标签或业务标签，需要选择分组：病人基础属性/业务属性；科室：无科室；分类：基础信息";
            }
        }
        if(tag_office != oid && tag_office != 8){// 科室
            return "您只能修改为您所在科室的标签";
        }
        if(role+1 < tag_weight){
            return "权限不足";
        }
        return "pass";
    }

    private String authEdit(int tag_group, int tag_office, int tag_weight, int oid, int role) {
        if(tag_group > role+1){// 分组
            return "您无法在修改该分组下标签";
        }
        if(tag_office != oid && tag_office != 8){// 科室
            return "您只能修改您所在科室的标签";
        }
        if(role+1 < tag_weight){
            return "权限不足";
        }
        return "pass";
    }



}
